 #include <iostream>
// #include <sys/stat.h>
// #include <sys/types.h>
// #include "logsink.hpp"
// #include "util.hpp"
// #include "level.hpp"
// #include "formatter.hpp"
// #include "message.hpp"
// #include "logger.hpp"
// #include <vector>
// #include "buffer.hpp"

//#include "chlog.hpp"

// void text()
// {
// 	CH::Logger::prt logger=CH::GetGlobalLogger("sync_name");
// 	logger->debug("%s", "测试日志");
// 	logger->error("%s", "测试日志");
// 	logger->fatal("%s", "测试日志");
// 	logger->warn("%s", "测试日志");

// 	size_t cur_size=0,count=0;
// 	while(cur_size<1024*1024)
// 	{
// 		logger->fatal("测试日志-%d",count++);
// 		cur_size+=20;
// 	}

// }

// void text1()
// {
// 	DEBUG("%s","日志器");
// 	WARN("%s","日志器");
// 	ERROR("%s","日志器");
// 	INFO("%s","日志器");
// 	FATAL("%s","日志器");
// }

// int main()
// {

	// CH::LogMsg msg("main.txt","root","格式化功能1测试....",65,CH::LogLevel::value::INFO);
	// CH::Formater fmt("[%d{%H:%M:%S}][%t][%p][%c][%f:%l] %m%n");
	// std::string str=fmt.format(msg);

	// CH::LogSink::prt stdout_prt=CH::SinkFactory::create<CH::StdoutSink>();
	// CH::LogSink::prt filename_prt=CH::SinkFactory::create<CH::FileSink>("./code/text.txt");
	// stdout_prt->log(str.c_str(),str.size());
	// filename_prt->log(str.c_str(),str.size());

	// CH::LogSink::prt roll_prt=CH::SinkFactory::create<CH::RollSizeSink>("./path/text-",1024*1024);
	// size_t cur_size=0;
	// while(cur_size<1024*1024*10)
	//{
	//	roll_prt->log(str.c_str(),str.size());
	//	cur_size+=str.size();
	// }

	/*
	const std::string logger_name="synch_logger";
	CH::LogLevel::value limit_level=CH::LogLevel::value::DEBUG;
	CH::Formater::prt fmt(new CH::Formater());
	CH::LogSink::prt stdout_prt=CH::SinkFactory::create<CH::StdoutSink>();
	CH::LogSink::prt file_name=CH::SinkFactory::create<CH::FileSink>("./path/text.log");
	std::vector<CH::LogSink::prt> v={stdout_prt,file_name};
	CH::Logger::prt logger(new CH::SynchLogger(logger_name,limit_level,fmt,v));


	// std::unique_ptr<CH::LoggerBuilder> builder(new CH::LocalLoggerBuilder());
	// builder->buildLoggerName("sync_name");
	// builder->buildLoggerLevel(CH::LogLevel::value::DEBUG);
	// builder->buildLoggerFormatter("[%d{%H:%M:%S}][%t][%p] %m%n");
	// builder->buildLoggerSink<CH::FileSink>("./path/text.log");
	//builder->buildLoggerSink<CH::StdoutSink>();
	// builder->buildLoggerSink<CH::RollSizeSink>("./path/code-",1024*1024);

	//CH::Logger::prt logger = builder->build();

	// int count=1024;
	// while (count--)
	// {
	// 	logger->debug(__FILE__, __LINE__, "%s", "测试日志");
	// 	logger->error(__FILE__, __LINE__, "%s", "测试日志");
	// 	logger->fatal(__FILE__, __LINE__, "%s", "测试日志");
	// 	logger->warn(__FILE__, __LINE__, "%s", "测试日志");
	// }


		size_t cur_size=0,count=0;
		while(cur_size<1024*1024*10)
		{
			logger->fatal(__FILE__,__LINE__,"测试日志-%d",count++);
			cur_size+=20;
		}


	/*以二进制打开文件
	std::ifstream ifs("./path/text.log",std::ios::binary);
	if(ifs.is_open()==false) return -1;
	ifs.seekg(0,std::ios::end);//读写位置跳转到文件末尾
	size_t fsize=ifs.tellg();//获取当前读写位置相对起始位置的偏移量
	ifs.seekg(0,std::ios::beg);//重新跳转到起始位置
	std::string body;
	body.resize(fsize);
	//将文件的数据，放入body中
	ifs.read(&body[0],fsize);
	if(ifs.good()==false)
	{
		std::cout<<"read false"<<std::endl;
		return -1;
	}
	ifs.close();
	//将body中的数据一个一个的写进缓冲区中
	CH::Buffer buffer;
	for(int i=0;i<body.size();i++)
	{
		buffer.push(&body[i],1);
	}

	//打开文件，如果不存在就创建
	std::ofstream ofs("./path/tmp.log",std::ios::binary);
	ofs.write(buffer.begin(),buffer.readAbleSize());
	ofs.close();*/


	// std::unique_ptr<CH::LoggerBuilder> builder(new CH::GlobalLoggerBuilder());
	// builder->buildLoggerType(CH::LoggerType::LOGGER_ASYNCH);
	// builder->buildLoggerName("sync_name");
	// builder->buildLoggerLevel(CH::LogLevel::value::DEBUG);
	// builder->buildLoggerFormatter("[%d{%H:%M:%S}][%t][%p] %m%n");
	// builder->buildLoggerSink<CH::FileSink>("./path/text.log");
    // builder->buildLoggerSink<CH::StdoutSink>();
	// builder->build();
	// // //builder->buildLoggerSink<CH::RollSizeSink>("./path/code-",1024*1024);
	// text1();


	// int count=1024;
	// while (count--)
	// {
	// 	logger->debug(__FILE__, __LINE__, "%s", "测试日志");
	// 	logger->error(__FILE__, __LINE__, "%s", "测试日志");
	// 	logger->fatal(__FILE__, __LINE__, "%s", "测试日志");
	// 	logger->warn(__FILE__, __LINE__, "%s", "测试日志");
	// }




	//return 0;
//}




#include "util.hpp"
#include "level.hpp"
#include "formatter.hpp"
#include "message.hpp"
#include "logsink.hpp"
#include "logger.hpp"
#include <vector>
#include "chlog.hpp"

void text_util()
{
	//返回当前时间戳
	std::cout<<CH::Util::date::now()<<std::endl;

	//返回目录
	std::cout<<CH::Util::file::file_path("./parh/mymkdir/text.txt")<<std::endl;

	//创建该目录
	CH::Util::file::create_directory("./parh/mymkdir/text.txt");


}

void text_level()
{
	std::cout<<CH::LogLevel::tostring(CH::LogLevel::value::DEBUG)<<std::endl;
	std::cout<<CH::LogLevel::tostring(CH::LogLevel::value::ERROR)<<std::endl;
	std::cout<<CH::LogLevel::tostring(CH::LogLevel::value::FATAL)<<std::endl;
	std::cout<<CH::LogLevel::tostring(CH::LogLevel::value::INFO)<<std::endl;
	std::cout<<CH::LogLevel::tostring(CH::LogLevel::value::OFF)<<std::endl;
	std::cout<<CH::LogLevel::tostring(CH::LogLevel::value::WARN)<<std::endl;
}

string text_format()
{
	CH::LogMsg meg(__FILE__,"root",__LINE__,CH::LogLevel::value::DEBUG,"日志消息");
	CH::Formater fmt;

	//std::cout<<fmt.format(meg)<<std::endl;

	return fmt.format(meg);
}

//落地方向测试
void text_sink()
{
	std::string str=text_format();
	CH::LogSink::prt stdout_lsp=CH::SinkFactory::create<CH::StdoutSink>();
	CH::LogSink::prt file_lsp=CH::SinkFactory::create<CH::FileSink>("./path/text.log");
	CH::LogSink::prt roll_lsp=CH::SinkFactory::create<CH::RollSizeSink>("./path/code-",1024*1024);

	stdout_lsp->log(str.c_str(),str.size());
	file_lsp->log(str.c_str(),str.size());
	size_t cursize = 0;
    size_t count = 0;
    while (cursize < 1024 * 1024 * 10) {
        std::string tmp = std::to_string(count++) + str;
        roll_lsp->log(tmp.c_str(), tmp.size());
        cursize += tmp.size();
    }
}

//日志器测试
void text_logger()
{
	const std::string logger_name="sync_name";
	CH::LogLevel::value logger_level=CH::LogLevel::value::DEBUG;
	CH::Formater::prt fmt(new CH::Formater());
	CH::LogSink::prt stdout_lsp = CH::SinkFactory::create<CH::StdoutSink>();
    CH::LogSink::prt file_lsp = CH::SinkFactory::create<CH::FileSink>("./path/log.txt");
    CH::LogSink::prt roll_file_lsp = CH::SinkFactory::create<CH::RollSizeSink>("./path/roll-", 1024 * 1024);
    std::vector<CH::LogSink::prt> sinks = { stdout_lsp, file_lsp, roll_file_lsp };

	CH::Logger::prt logger_prt(new CH::SynchLogger(logger_name,logger_level,fmt,sinks));

    logger_prt->debug(__FILE__, __LINE__, "%s", "测试日志");
    logger_prt->info (__FILE__, __LINE__, "%s", "测试日志");
    logger_prt->warn (__FILE__, __LINE__, "%s", "测试日志");
    logger_prt->error(__FILE__, __LINE__, "%s", "测试日志");
    logger_prt->fatal(__FILE__, __LINE__, "%s", "测试日志");

    size_t cursize = 0, count = 0;
    std::string str = "测试日志";
    while (cursize < 1024 * 1024 * 10) {
        logger_prt->fatal(__FILE__, __LINE__, "测试日志-%d", count++);
        cursize += 20;
    }

}

//同步局部日志器测试
void text_LocalLoggerBuilder_synch()
{
	CH::LoggerBuilder::prt build(new CH::LocalLoggerBuilder());
	build->buildLoggerName("local_logger");
	build->buildLoggerLevel(CH::LogLevel::value::DEBUG);
	build->buildLoggerFormatter("%m%n");
	build->buildLoggerType(CH::LoggerType::LOGGER_SYNCH);
	build->buildLoggerSink<CH::StdoutSink>();
	build->buildLoggerSink<CH::FileSink>("./path/text.log");
	build->buildLoggerSink<CH::RollSizeSink>("./path/code-",1024*1024);

	CH::Logger::prt logger_prt=build->build();

	logger_prt->debug(__FILE__, __LINE__, "%s", "测试日志");
    logger_prt->info (__FILE__, __LINE__, "%s", "测试日志");
    logger_prt->warn (__FILE__, __LINE__, "%s", "测试日志");
    logger_prt->error(__FILE__, __LINE__, "%s", "测试日志");
    logger_prt->fatal(__FILE__, __LINE__, "%s", "测试日志");

    size_t cursize = 0, count = 0;
    std::string str = "测试日志";
    while (cursize < 1024 * 1024 * 3) {
        logger_prt->fatal(__FILE__, __LINE__, "测试日志-%d", count++);
        cursize += 20;
	}
}

//异步局部日志器测试
void text_LocalLoggerBuilder_asynch() {
    CH::LoggerBuilder::prt builder(new CH::LocalLoggerBuilder());
    builder->buildLoggerType(CH::LoggerType::LOGGER_ASYNCH);
    builder->buildLoggerLevel(CH::LogLevel::value::WARN);
    builder->buildLoggerName("clx_asynch_logger");
    builder->buildLoggerFormatter("%m%n");
    builder->buildLoggerSink<CH::StdoutSink>();
    builder->buildLoggerSink<CH::FileSink>("./path/test.log");
    builder->buildLoggerSink<CH::RollSizeSink>("./path/roll-", 1024 * 1024);
    CH::Logger::prt logger_ptr = builder->build();

    logger_ptr->debug(__FILE__, __LINE__, "%s", "测试日志");
    logger_ptr->info (__FILE__, __LINE__, "%s", "测试日志");
    logger_ptr->warn (__FILE__, __LINE__, "%s", "测试日志");
    logger_ptr->error(__FILE__, __LINE__, "%s", "测试日志");
    logger_ptr->fatal(__FILE__, __LINE__, "%s", "测试日志");

    size_t cursize = 0, count = 0;
    std::string str = "测试日志";
    while (cursize < 1024 * 1024) {
        logger_ptr->fatal(__FILE__, __LINE__, "测试日志-%d", count++);
        cursize += 20;
    }
}

//全局日志器测试
void text_global_LoggerManager() {
    CH::LoggerBuilder::prt builder(new CH::GlobalLoggerBuilder());
    builder->buildLoggerType(CH::LoggerType::LOGGER_ASYNCH);
    builder->buildLoggerLevel(CH::LogLevel::value::WARN);
    builder->buildLoggerName("clx_asynch_logger");
    builder->buildLoggerFormatter("%m%n");
    builder->buildLoggerSink<CH::StdoutSink>();
    builder->buildLoggerSink<CH::FileSink>("./path/test.log");
    builder->buildLoggerSink<CH::RollSizeSink>("./path/roll-", 1024 * 1024);
    builder->build();

    CH::Logger::prt logger_ptr = CH::LoggerManager::GetInstance().GetLogger("clx_asynch_logger");
    logger_ptr->debug(__FILE__, __LINE__, "%s", "测试日志");
    logger_ptr->info (__FILE__, __LINE__, "%s", "测试日志");
    logger_ptr->warn (__FILE__, __LINE__, "%s", "测试日志");
    logger_ptr->error(__FILE__, __LINE__, "%s", "测试日志");
    logger_ptr->fatal(__FILE__, __LINE__, "%s", "测试日志");

    size_t count = 0;
    std::string str = "测试日志";
    while (count < 1e5 * 5) {
        logger_ptr->fatal(__FILE__, __LINE__, "测试日志-%d", count++);
    }
}

void text_chlog() {
    CH::LoggerBuilder::prt builder(new CH::GlobalLoggerBuilder());
    builder->buildLoggerLevel(CH::LogLevel::value::DEBUG);
    builder->buildLoggerName("clx_synch_logger");
    builder->build();
    CH::Logger::prt logger_ptr = CH::LoggerManager::GetInstance().GetLogger("clx_synch_logger");

    INFO("%s", "测试开始");

    DEBUG("%s", "测试日志");
    INFO("%s", "测试日志");
    WARN("%s", "测试日志");
    ERROR("%s", "测试日志");
    FATAL("%s", "测试日志");

    INFO("%s", "测试完毕");

}



int main()
{
	//text_util();
	//text_level();
	//text_format();
	//text_sink();
	//text_logger();
	//text_LocalLoggerBuilder();
	//text_LocalLoggerBuilder_asynch();
	//text_global_LoggerManager();
	text_chlog();
	return 0;
}

